/**
 * Created by Tuffy on 16/1/18.
 */
'use strict';

angular.module('starter.controllers')

.controller('ContactListCtrl', ['$scope', '$ionicActionSheet', '$state','$timeout', 'ContactService', function($scope, $ionicActionSheet, $state,$timeout, ContactService) {
    var hotelId = $state.params.hotelId;

    $scope.title = '酒店联系人';
    // 显示酒店明细
    $scope.showHotelDetail = function(hotelId) {
        $state.go('app.hotel-detail', {hotelId: hotelId});
    };

    /**
     * contact
     */
    $scope.contacts ={
        search: {
            value:'',
            doAct:function(){
                $scope.contacts.page.pageNum = 1;
                if(this.timer){
                    $timeout.cancel(this.timer);
                }
                this.timer = $timeout(function(){
                    $scope.contacts.data = [];
                    $scope.contacts.loadData();
                },500);
            }
        },
        page: {
            pageNum:1,
            pageSize:10,
            total:200
        },
        loaded:false,
        data:[],
        loadData:function(okFunc){
            var _this = this,
                params = {
                    hotelId:hotelId,
                    hotelName:_this.search.value,
                    pageNum:_this.page.pageNum,
                    pageSize:_this.page.pageSize
                };
            _this.loaded = false;
            for(var key in _this.search.filter){
                params[key] = _this.search.filter[key];
            }
            ContactService.findList(params)
                .success(function(res){
                    if(res.success){
                        if(typeof (okFunc) == 'function'){
                            okFunc();
                        }
                        _this.data = _this.data.concat(res.attribute.rows);
                        _this.page.total = res.attribute.total;
                    }else{
                        _this.page.total = 0;
                        console.log('加载失败');
                    }
                }).error(function(error){
                    _this.page.total = 0;
                    console.log(error);
                }).finally(function(){
                    _this.loaded = true;
                    $scope.$broadcast('scroll.refreshComplete');
                    $scope.$broadcast('scroll.infiniteScrollComplete');
                });
        },
        moreDataCanBeLoaded:function(){
            return this.page.total > this.page.pageNum*this.page.pageSize;
        },
        loadMore:function(){
            this.page.pageNum++;
            this.loadData();
        },
        refresh:function(){
            var _this = this;
            _this.page.pageNum = 1;
            _this.loadData(function(){
                _this.data = [];
            });
        }
    } ;
    $scope.contactDict = {
        '1':'老板',
        '2':'前台',
        '3':'经理',
        '4':'其他'
    };
    $scope.contacts.loadData();
}])

.service('ContactService',['CrmHttp','$rootScope',function(CrmHttp, $rootScope){
    var url = '/crm/crm-hotel-role/';
    return {
        /**
         * 用户通讯录
         * @param params
         * @returns {*}
         */
        findList: function (params) {
            var act = 'find-list';
            params['userId'] = $rootScope.localStorageObj.userId;
            return CrmHttp.post(url + act, params);
        },
        /**
         * 增加新的联系人
         * @param params
         * @returns {*}
         */
        insert : function(params){
            var act = 'insert';
            params['userId'] = $rootScope.localStorageObj.userId;
            return CrmHttp.post(url + act, params);
        },
        /**
         * 修改联系人
         * @param params
         * @returns {*}
         */
        update : function(params){
            var act = 'update';
            params['userId'] = $rootScope.localStorageObj.userId;
            return CrmHttp.post(url + act, params);
        }
    };
}]);